from django.db import models
from django.utils import timezone


class Material(models.Model):
    """物料模型"""
    name = models.CharField('物料名称', max_length=100)
    code = models.CharField('物料编码', max_length=50, unique=True)
    specification = models.CharField('规格型号', max_length=200, blank=True)
    unit = models.CharField('单位', max_length=20, default='件')
    price = models.DecimalField('单价', max_digits=10, decimal_places=2, default=0)
    created_at = models.DateTimeField('创建时间', default=timezone.now)
    is_active = models.BooleanField('是否启用', default=True)

    class Meta:
        verbose_name = '物料'
        verbose_name_plural = '物料管理'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.name} ({self.code})"


class Device(models.Model):
    """设备模型"""
    STATUS_CHOICES = [
        ('running', '运行中'),
        ('stopped', '停机'),
        ('maintenance', '维护中'),
        ('fault', '故障'),
    ]
    
    name = models.CharField('设备名称', max_length=100)
    code = models.CharField('设备编码', max_length=50, unique=True)
    location = models.CharField('设备位置', max_length=100, blank=True)
    status = models.CharField('设备状态', max_length=20, choices=STATUS_CHOICES, default='stopped')
    install_date = models.DateField('安装日期', blank=True, null=True)
    last_maintenance = models.DateField('上次维护', blank=True, null=True)
    created_at = models.DateTimeField('创建时间', default=timezone.now)
    is_active = models.BooleanField('是否启用', default=True)

    class Meta:
        verbose_name = '设备'
        verbose_name_plural = '设备管理'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.name} ({self.code})"


class WorkOrder(models.Model):
    """工单模型"""
    STATUS_CHOICES = [
        ('pending', '待处理'),
        ('in_progress', '进行中'),
        ('completed', '已完成'),
        ('cancelled', '已取消'),
    ]
    
    order_no = models.CharField('工单号', max_length=50, unique=True)
    material = models.ForeignKey(Material, on_delete=models.CASCADE, verbose_name='物料')
    device = models.ForeignKey(Device, on_delete=models.CASCADE, verbose_name='设备')
    quantity = models.PositiveIntegerField('数量', default=1)
    status = models.CharField('工单状态', max_length=20, choices=STATUS_CHOICES, default='pending')
    start_time = models.DateTimeField('开始时间', blank=True, null=True)
    end_time = models.DateTimeField('结束时间', blank=True, null=True)
    created_at = models.DateTimeField('创建时间', default=timezone.now)
    notes = models.TextField('备注', blank=True)

    class Meta:
        verbose_name = '工单'
        verbose_name_plural = '工单管理'
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.order_no} - {self.material.name}"


class QualityCheck(models.Model):
    """质检模型"""
    RESULT_CHOICES = [
        ('pass', '合格'),
        ('fail', '不合格'),
        ('pending', '待检'),
    ]
    
    work_order = models.ForeignKey(WorkOrder, on_delete=models.CASCADE, verbose_name='工单')
    check_item = models.CharField('检验项目', max_length=100)
    result = models.CharField('检验结果', max_length=20, choices=RESULT_CHOICES, default='pending')
    check_value = models.CharField('检验值', max_length=100, blank=True)
    standard_value = models.CharField('标准值', max_length=100, blank=True)
    checker = models.CharField('检验员', max_length=50, blank=True)
    check_time = models.DateTimeField('检验时间', default=timezone.now)
    notes = models.TextField('检验备注', blank=True)

    class Meta:
        verbose_name = '质检记录'
        verbose_name_plural = '质检管理'
        ordering = ['-check_time']

    def __str__(self):
        return f"{self.work_order.order_no} - {self.check_item}" 